<?php
namespace Extend\Controller;

/**
 * 客栈实时房态相关rpc接口
 * @modify trainyao 2016-06-23 创建
 */
class RoomController extends ParentController {
    protected function _initialize() { 

        $this->validateUtil = new \Org\Util\Validate();
    }

    /**
     * 获取客栈所有房型实时房态
     * @modify trainyao 2016-05-04    创建
     * @modify trainyao 2016-07-07    增加预定模式返回
     */
    public function roomTypes($data) {
        // 设置时区
        parent::switchTimeUtc($data);

        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'startDate' => 'notnull,notempty,isDate',
            'endDate' => 'notnull,notempty,isDate',
        );

        if (isset($data['transCurrencyId']) && !empty($data['transCurrencyId'])) {
            $validateRule['transCurrencyId'] = 'notnull,notempty,id';
        }
        $validatePass = $this->validateUtil->validate($data, $validateRule);
        if (empty($validatePass)) {
            $message = $this->validateUtil->getError('explode', ',');
            return parent::returnJson($message['msg'][0], $message['msg'][1]);
        }
        
        $today = date('Y-m-d');
        $startDate = $data['startDate'];
        $endDate = $data['endDate'];
        // 是否从钥匙包进入
        $qrclietBook = $data['qrclietBook'];
        // 是否微信浏览器
        $isWechatBrowser = $data['isWechatBrowser'];
        $transCurrencyId = isset($data['transCurrencyId']) && !empty($data['transCurrencyId']) ? $data['transCurrencyId'] : null;
        $bookingLangSet = $data['bookingLangSet'];

        // 校验时间合法性
        if (isBeforeTime($startDate, $today) || isBeforeTime($endDate, $startDate)) {
            return parent::returnJson(403, 202);
        }

        // 检查客栈是否已经开通微预定
        $hotelId = $data['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId, false);
        if (empty($isEnable)) {
            return parent::returnJson(403, 409);
        }

        // 获取客栈所有房型实时房态
        $roomTypeBookLogic = A('Common/RoomTypeBook', 'Logic');
        try {
            $roomTypeInfo = $roomTypeBookLogic->getRealtimeStatus($hotelId, $startDate, $endDate);
        } catch (\Exception $e) {
            $errorMsg = explode(',', $e->getMessage());
            return parent::returnJson($errorMsg[0], $errorMsg[1]);
        }
        
        // 获取客栈支付方式
        /*$hotelPayDetailModel = D('HotelPayDetail');
        $hotelPayDetail = $hotelPayDetailModel->getInfoByHotel($hotelId);
        $isOffline = true;
        if (! empty($hotelPayDetail['type']) && $hotelPayDetail['type']>0){
            $isOffline = false;
        }*/

        // 是否开启支付方式 (是否线下付)
        $isOffline = $this->isOffline($hotelId, $qrclietBook, $isWechatBrowser);


        // 开启畅联代收  才计算义卖价
        $isPayUclbrt = $this->checkIsUclbrtPay($hotelId);

        // 处理数据
        foreach ($roomTypeInfo as &$roomTypeItem) {
            sort($roomTypeItem['priceList'], SORT_NUMERIC);
            sort($roomTypeItem['shopPriceList'], SORT_NUMERIC);
            // 如果预定价格等于门市价格 则只取预定价格
            $shopPrice = $roomTypeItem['priceList'][0] == $roomTypeItem['shopPriceList'][0] ? '' : $roomTypeItem['shopPriceList'][0];
            $charityPrice = $isPayUclbrt == true ? $roomTypeItem['charityprice'] : '0.00';
            $roomTypeItem = array(
                'id' => $roomTypeItem['roomtype_id'],
                'coverImageUrl' => MEIZHU_URL . $roomTypeItem['coverimageurl'],
                'name' => $roomTypeItem['name'],
                'price' => $roomTypeItem['priceList'][0],
                'shopPrice' => $shopPrice,
                'rest' => $roomTypeItem['rest'],
                'description' => $roomTypeItem['description'],
                'isOffline' => $isOffline,
                'lastRoom' => $roomTypeItem['lastRoom'] ? true : false,
                'charityPrice' => (float)$charityPrice,
                'minustype' => $roomTypeItem['minustype'],
            );
        }
        unset($roomTypeItem);

        // 晚数
        $nights = getNight($startDate, $endDate);
        // 获取客栈信息
        $hotelDetail = $hotelBookLogic->getInfo($hotelId, 'brief');

        // 获取客栈货币
        $currencyLogic = A('Common/Currency', 'Logic');
        $hotelCurrencyInfo = $currencyLogic->takeHotelCurrency($hotelId, false, $bookingLangSet);

        // 价格汇率转换
        if (!empty($hotelCurrencyInfo) && !is_null($transCurrencyId) && !empty($roomTypeInfo)) {
            // 取出默认货币
            $defaultCurrency = my_sort($hotelCurrencyInfo, 'type', SORT_DESC, SORT_NUMERIC);
            $currencyId = $defaultCurrency[0]['type'] == 1 ? $defaultCurrency[0]['currencyId'] : 8 ; // 8 = 人民币
            if ($currencyId != $transCurrencyId) {
                $roomTypeInfo = $currencyLogic->exchangeRates($currencyId, $transCurrencyId, $roomTypeInfo, 'price');
                $roomTypeInfo = $currencyLogic->exchangeRates($currencyId, $transCurrencyId, $roomTypeInfo, 'shopPrice');
            }
        }

        return parent::returnJson(200, 200, array(
            'hotel' => $hotelId,
            'hotelName' => $hotelDetail['name'],
            'bookMode' => $hotelDetail['bookmode'],
            'startDate' => $startDate,
            'nights' => $nights,
            'roomTypes' => $roomTypeInfo,
            'hotelCurrency' => $hotelCurrencyInfo,
        ));
    }

    /**
     * 获取客栈所有房型实时房态
     * @modify trainyao 2016-05-04    创建
     * @modify trainyao 2016-07-07    增加预定模式返回,根据预定模式是否返回房间实时房态
     */
    public function roomTypeDetail($data) {
        // 设置时区
        parent::switchTimeUtc($data);

        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'startDate' => 'notnull,notempty,isDate',
            'endDate' => 'notnull,notempty,isDate',
            'roomType' => 'notnull,notempty,id',
        );
        $validatePass = $this->validateUtil->validate($data, $validateRule);
        if (empty($validatePass)) {
            $message = $this->validateUtil->getError('explode', ',');
            return parent::returnJson($message['msg'][0], $message['msg'][1]);
        }

        $today = date('Y-m-d');
        $startDate = $data['startDate'];
        $endDate = $data['endDate'];
        // 校验时间合法性
        if (isBeforeTime($startDate, $today) || isBeforeTime($endDate, $startDate)) {
            return parent::returnJson(403, 222);
        }

        // 检查客栈是否已经开通微预定
        $hotelId = $data['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId, false);
        if (empty($isEnable)) {
            return parent::returnJson(403, 409);
        }

        // 检查房型是否可预定
        $roomTypeId = explode(',', $data['roomType']);
        $roomTypeBookLogic = A('Common/RoomTypeBook', 'Logic');
        $canBook = $roomTypeBookLogic->checkBookable($hotelId, $roomTypeId);
        if (empty($canBook)) {
            return parent::returnJson(403, 304);
        }
        
        // 获取客栈单个房型实时房态
        try {
            $roomTypeInfo = $roomTypeBookLogic->getRealtimeStatus($hotelId, $startDate, $endDate, $roomTypeId);
        } catch (\Exception $e) {
            $errorMsg = explode(',', $e->getMessage());
            return parent::returnJson($errorMsg[0], $errorMsg[1]);
        }

        $roomTypeInfo[0]['gallery'] = array_column($roomTypeInfo[0]['gallery'], 'name');
        foreach ($roomTypeInfo[0]['gallery'] as &$urlItem) {
            $urlItem = MEIZHU_URL . $urlItem;
        }
        unset($urlItem);

        $hotelPayDetailModel = D('HotelPayDetail');
        $hotelPayDetail = $hotelPayDetailModel->getInfoByHotel($hotelId);
        // 开启畅联代收  才计算义卖价
        $isPayUclbrt = $this->checkIsUclbrtPay($hotelId);
        $charityPrice = $isPayUclbrt == true ? $roomTypeInfo[0]['charityprice'] : '0.00';
        // 获取客栈信息
        $hotelDetail = $hotelBookLogic->getInfo($hotelId, 'brief');

        // 价格
        sort($roomTypeInfo[0]['priceList'], SORT_NUMERIC);

        $returnData = array(
            'hotel' => $hotelId,
            'hotelName' => $hotelDetail['name'],
            'bookMode' => $hotelDetail['bookmode'],
            'roomType' => $roomTypeInfo[0]['roomtype_id'],
            'roomTypeName' => $roomTypeInfo[0]['name'],
            'gallery' => $roomTypeInfo[0]['gallery'],
            'description' => $roomTypeInfo[0]['description'],
            'charityPrice' => (float)$charityPrice,
            'minusdiscount' => $roomTypeInfo[0]['minusdiscount'],
            'minustype' => $roomTypeInfo[0]['minustype'],
            'price' => number_format($roomTypeInfo[0]['priceList'][0], 2, '.', ''),
        );

        // 房间预定模式,处理数据
        if ($hotelDetail['bookmode'] == BOOK_MODE_ROOM) {
            foreach ($roomTypeInfo[0]['room'] as &$roomItem) {
                $roomItem = array(
                    'enable' => $roomItem['canBook'],
                    'id' => $roomItem['id'],
                    'name' => $roomItem['name'],
                    'cardType' => $roomItem['roomfrom'],
                    'isDirty' => $roomItem['isdirty'],
                );
            }
            unset($roomItem);
            $returnData['rooms'] = $roomTypeInfo[0]['room'];
        }

        return parent::returnJson(200, 200, $returnData);
    }

    /**
     * 获取单个房间实时房态
     * @modify trainyao 2016-05-05    创建
     * @modify trainyao 2016-07-07    更名bookDetail,改为可不传房间ID,查看房型预定信息
     * @modify wenhui 2017-02-16     可以同时预定多个房间（最多三个）修改返回参数格式（支持返回多个房间）
     */
    public function bookDetail($data) {
        // 设置时区
        parent::switchTimeUtc($data);

        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'startDate' => 'notnull,notempty,isDate',
            'endDate' => 'notnull,notempty,isDate',
            'roomType' => 'notnull,notempty,id',
        );
        $validatePass = $this->validateUtil->validate($data, $validateRule);
        if (empty($validatePass)) {
            $message = $this->validateUtil->getError('explode', ',');
            return parent::returnJson($message['msg'][0], $message['msg'][1]);
        }

        if (isset($data['room']) && is_array($data['room'])) {
            // 最多三个房间
            if (count($data['room']) > 3) {
                return parent::returnJson(403, 605);
            }

            $validateRule = array(
                'room' => 'notempty,id',
            );
            $validatePass = $this->validateUtil->validate($data['room'], $validateRule);
            if (empty($validatePass)) {
                $message = $this->validateUtil->getError('explode', ',');
                return parent::returnJson($message['msg'][0], $message['msg'][1]);
            }
        }

        $today = date('Y-m-d');
        $startDate = $data['startDate'];
        $endDate = $data['endDate'];
        // 校验时间合法性
        if (isBeforeTime($startDate, $today) || isBeforeTime($endDate, $startDate)) {
            return parent::returnJson(403, 202);
        }
        
        // 检查客栈是否已经开通微预定
        $hotelId = $data['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId, false);
        if (empty($isEnable)) {
            return parent::returnJson(403, 409);
        }

        // 检查房型是否可预定
        $roomTypeId = explode(',', $data['roomType']);
        $roomTypeBookLogic = A('Common/RoomTypeBook', 'Logic');
        $canBook = $roomTypeBookLogic->checkBookable($hotelId, $roomTypeId);
        if (empty($canBook)) {
            return parent::returnJson(403, 304);
        }
        
        // 检查房型房间是否对应
        $roomId = null;
        if (isset($data['room'])) {
            $roomId = $data['room'];
            $roomInRoomType = $roomTypeBookLogic->checkRoomInRoomType($hotelId, $roomTypeId[0], $roomId);
            if (empty($roomInRoomType)) {
                return parent::returnJson(403, 202);
            }
            //$roomId = explode(',', $roomId);
        }

        // 获取房间实时房态
        try {
            $roomInfo = $roomTypeBookLogic->getRealtimeStatus($hotelId, $startDate, $endDate, $roomTypeId, $roomId);
        } catch (\Exception $e) {
            $errorMsg = explode(',', $e->getMessage());
            return parent::returnJson($errorMsg[0], $errorMsg[1]);
        }

        // 根据是否传房间ID,分别处理查询是房型还是房间是否可以预定
        if (is_null($roomId)) {
            if ($roomInfo[0]['rest'] == 0) {
                return parent::returnJson(403, 307);
            }
        } else {
            foreach ($roomInfo[0]['room'] as $key => $roomItem) {
                if ($roomItem['canBook'] == DISABLE) {
                    return parent::returnJson(403, 306);
                }
            }
        }

        // 获取客栈信息
        $hotelDetail = $hotelBookLogic->getInfo($hotelId, 'brief');

        // 获取客栈设置押金
        $hotelPayDetailModel = D('HotelPayDetail');
        $hotelPayDetail = $hotelPayDetailModel->getInfoByHotel($hotelId);
        // 获取每天不同价格之和
        $totalPrice = getAllPrice($roomInfo[0]['roomtype_id'],$startDate,$endDate);
        $totalPrice = $totalPrice['totalPrice'];
        // 多个房间 计算总价格 = 预定的房间数 * 一个房间每天不同价格之和
        if (isset($data['room']) && is_array($roomId)) {
            $totalPrice = $totalPrice * count($roomId);
            $hotelPayDetail['deposit'] = $hotelPayDetail['deposit'] * count($roomId);
        }

        // 开启畅联代收  才计算义卖价
        $isPayUclbrt = $this->checkIsUclbrtPay($hotelId);

        $charityPrice = $isPayUclbrt === true ? $roomInfo[0]['charityprice'] : '0.00';
        $night = getNight($startDate, $endDate);
        if (isset($data['room']) && is_array($roomId)) {
            $charityPrice = $charityPrice * count($roomId) * $night;
        }

        // 计算房费立减优惠
        if ($roomInfo[0]['minustype'] == 1) {
            // 立减优惠金额
            $minusPrice = $roomInfo[0]['minusdiscount'];
        } else if ($roomInfo[0]['minustype'] == 2) {
            // 立减优惠金额按比例优惠
            $minusPrice = ($totalPrice - $charityPrice) * ($roomInfo[0]['minusdiscount']/100);
        } else {
            $minusPrice = 0;
        }

        // 获取客栈所有房型
        $roomTypLogic = A('Common/RoomType', 'Logic');
        $roomTypInfo = $roomTypLogic->getInfoByHotel($hotelId);
        $minusTypes = array_column($roomTypInfo, 'minustype');
        rsort($minusTypes, SORT_NUMERIC);
        // 是否有立减优惠
        $minusenable = $minusTypes[0] == 0 ? 0 : $hotelDetail['minusenable'];

        // 根据是否查看房间,返回不同信息
        $returnData = array(
            'hotel' => $hotelId,
            'hotelName' => $hotelDetail['name'],
            'bookMode' => $hotelDetail['bookmode'],
            'minusEnable' => $minusenable,
            'minusPrice' => (float)$minusPrice,
            'roomType' => $roomInfo[0]['roomtype_id'],
            'roomTypeName' => $roomInfo[0]['name'],
            'price' => (float)$totalPrice,
            'deposit' => (float)$hotelPayDetail['deposit'],
            'charityPrice' => (float)$charityPrice,
            'startDate' => $startDate,
            'nights' => $night,
        );
        if (!is_null($roomId)) {
            $returnData['rooms'] = array();
            foreach ($roomInfo[0]['room'] as $key => $roomItem) {
                $roomInfos = array(
                    'room' => $roomItem['id'],
                    'roomName' => $roomItem['name'],
                );
                array_push($returnData['rooms'], $roomInfos);
            }
        }
        return parent::returnJson(200, 200, $returnData);
    }

    /**
     * 获取客栈所有房型实时房态
     * @modify trainyao 2016-05-04    创建
     * @modify trainyao 2016-07-07    增加预定模式返回
     */
    public function hourRoomTypes($data) {
        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }
        parent::switchTimeUtc($data);
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'startDate' => 'notnull,notempty,isDate',
            'endDate' => 'notnull,notempty,isDate',
            'hour' => 'notnull,notempty,natureNumber'
        );
        if (isset($data['transCurrencyId']) && !empty($data['transCurrencyId'])) {
            $validateRule['transCurrencyId'] = 'notnull,notempty,id';
        }
        $validatePass = $this->validateUtil->validate($data, $validateRule);
        if (empty($validatePass)) {
            $message = $this->validateUtil->getError('explode', ',');
            return parent::returnJson($message['msg'][0], $message['msg'][1]);
        }
        
        $today = date('Y-m-d');
        $startDate = $data['startDate'];
        $endDate = $data['endDate'];
        // 是否从钥匙包进入
        $qrclietBook = $data['qrclietBook'];
        // 是否微信浏览器
        $isWechatBrowser = $data['isWechatBrowser'];
        $transCurrencyId = isset($data['transCurrencyId']) && !empty($data['transCurrencyId']) ? $data['transCurrencyId'] : null;
        $bookingLangSet = $data['bookingLangSet'];

        // 校验时间合法性
        if (isBeforeTime($startDate, $today) || isBeforeTime($endDate, $startDate)) {
            return parent::returnJson(403, 202);
        }

        // 检查客栈是否已经开通微预定
        $hotelId = $data['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId, false);
        if (empty($isEnable)) {
            return parent::returnJson(403, 409);
        }
        $hour = $data['hour'];
        // 获取客栈所有房型实时房态
        $hourRoomLogic = A('Common/HourRoom', 'Logic');
        try {
            $roomTypeInfo = $hourRoomLogic->getRealtimeStatus($hotelId, $startDate, $endDate, $hour);
        } catch (\Exception $e) {
            $errorMsg = explode(',', $e->getMessage());
            return parent::returnJson($errorMsg[0], $errorMsg[1]);
        }
        
        /*// 获取客栈支付方式
        $hotelPayDetailModel = D('HotelPayDetail');
        $hotelPayDetail = $hotelPayDetailModel->getInfoByHotel($hotelId);
        $isOffline = true;
        if (! empty($hotelPayDetail['type']) && $hotelPayDetail['type']>0){
            $isOffline = false;
        }*/

        // 是否开启支付方式 (是否线下付)
        $isOffline = $this->isOffline($hotelId, $qrclietBook, $isWechatBrowser);

        // 处理数据
        foreach ($roomTypeInfo as &$roomTypeItem) {
            sort($roomTypeItem['priceList'], SORT_NUMERIC);
            sort($roomTypeItem['shopPriceList'], SORT_NUMERIC);
            // 如果预定价格等于门市价格 则只取预定价格
            $shopPrice = $roomTypeItem['priceList'][0] == $roomTypeItem['shopPriceList'][0] ? '' : $roomTypeItem['shopPriceList'][0];
            //$charityPrice = $isPayUclbrt == true ? $roomTypeItem['charityprice'] : '0.00';
            $roomTypeItem = array(
                'id' => $roomTypeItem['roomtype_id'],
                'coverImageUrl' => MEIZHU_URL . $roomTypeItem['coverimageurl'],
                'name' => $roomTypeItem['name'],
                'rest' => $roomTypeItem['rest'],
                'description' => $roomTypeItem['description'],
                'isOffline' => $isOffline,
                'price' => $roomTypeItem['price'],
                'charityPrice' => 0,
            );
        }
        unset($roomTypeItem);

        // 获取客栈信息
        $hotelDetail = $hotelBookLogic->getInfo($hotelId, 'brief');

        // 获取客栈货币
        $currencyLogic = A('Common/Currency', 'Logic');
        $hotelCurrencyInfo = $currencyLogic->takeHotelCurrency($hotelId, false, $bookingLangSet);

        // 价格汇率转换
        if (!empty($hotelCurrencyInfo) && !is_null($transCurrencyId) && !empty($roomTypeInfo)) {
            // 取出默认货币
            $defaultCurrency = my_sort($hotelCurrencyInfo, 'type', SORT_DESC, SORT_NUMERIC);
            $currencyId = $defaultCurrency[0]['type'] == 1 ? $defaultCurrency[0]['currencyId'] : 8 ; // 8 = 人民币
            if ($currencyId != $transCurrencyId) {
                $roomTypeInfo = $currencyLogic->exchangeRates($currencyId, $transCurrencyId, $roomTypeInfo, 'price');
            }
        }

        return parent::returnJson(200, 200, array(
            'hotel' => $hotelId,
            'hotelName' => $hotelDetail['name'],
            'bookMode' => $hotelDetail['bookmode'],
            'roomTypes' => $roomTypeInfo,
            'hotelCurrency' => $hotelCurrencyInfo,
        ));
    }

    /**
     * 获取客栈所有房型实时房态
     * @modify zhangzhen 2017-06-01    创建
     */
    public function hourRoomTypeDetail($data) {
        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }
        parent::switchTimeUtc($data);
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'startTime' => 'notnull,notempty',
            'hour' => 'notnull,notempty',
            'roomType' => 'notnull,notempty,id',
        );
        $validatePass = $this->validateUtil->validate($data, $validateRule);
        if (empty($validatePass)) {
            $message = $this->validateUtil->getError('explode', ',');
            return parent::returnJson($message['msg'][0], $message['msg'][1]);
        }

        $startDate = date('Y-m-d');
        $endDate = date('Y-m-d',strtotime('+1 day'));
        // 检查客栈是否已经开通微预定
        $hotelId = $data['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId, false);
        if (empty($isEnable)) {
            return parent::returnJson(403, 409);
        }
        // 检查房型是否可预定
        $roomTypeId = $data['roomType'];
        $roomTypeBookLogic = A('Common/RoomTypeBook', 'Logic');
        $canBook = $roomTypeBookLogic->checkBookable($hotelId, explode(',', $data['roomType']));
        if (empty($canBook)) {
            return parent::returnJson(403, 304);
        }
        $hour = $data['hour'];
        // 获取客栈所有房型实时房态
        $hourRoomLogic = A('Common/HourRoom', 'Logic');
        // 获取客栈单个房型实时房态
        try {
            $roomTypeInfo = $hourRoomLogic->getRealtimeStatus($hotelId, $startDate, $endDate, $hour, true, $roomTypeId);
        } catch (\Exception $e) {
            $errorMsg = explode(',', $e->getMessage());
            return parent::returnJson($errorMsg[0], $errorMsg[1]);
        }

        $roomTypeInfo[0]['gallery'] = array_column($roomTypeInfo[0]['gallery'], 'name');
        foreach ($roomTypeInfo[0]['gallery'] as &$urlItem) {
            $urlItem = MEIZHU_URL . $urlItem;
        }
        unset($urlItem);

        $hotelPayDetailModel = D('HotelPayDetail');
        $hotelPayDetail = $hotelPayDetailModel->getInfoByHotel($hotelId);

        // 获取客栈信息
        $hotelDetail = $hotelBookLogic->getInfo($hotelId, 'brief');

        $returnData = array(
            'hotel' => $hotelId,
            'hotelName' => $hotelDetail['name'],
            'bookMode' => $hotelDetail['bookmode'],
            'roomType' => $roomTypeInfo[0]['roomtype_id'],
            'roomTypeName' => $roomTypeInfo[0]['name'],
            'gallery' => $roomTypeInfo[0]['gallery'],
            'description' => $roomTypeInfo[0]['description'],
            'price' => $roomTypeInfo[0]['price'],
            'charityPrice' => 0,
        );

        // 房间预定模式,处理数据
        if ($hotelDetail['bookmode'] == BOOK_MODE_ROOM) {
            foreach ($roomTypeInfo[0]['room'] as &$roomItem) {
                $roomItem = array(
                    'enable' => $roomItem['canBook'],
                    'id' => $roomItem['id'],
                    'name' => $roomItem['name'],
                    'cardType' => $roomItem['roomfrom'],
                    'isDirty' => $roomItem['isdirty'],
                );
            }
            unset($roomItem);
            $returnData['rooms'] = $roomTypeInfo[0]['room'];
        }

        return parent::returnJson(200, 200, $returnData);
    }

    /**
     * 获取房间实时房态
     * @modify zhangzhen 2017-06-01    创建
     */
    public function hourBookDetail($data) {
        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }
        parent::switchTimeUtc($data);
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'startTime' => 'notnull,notempty',
            'hour' => 'notnull,notempty',
            'roomType' => 'notnull,notempty,id',
        );
        $validatePass = $this->validateUtil->validate($data, $validateRule);
        if (empty($validatePass)) {
            $message = $this->validateUtil->getError('explode', ',');
            return parent::returnJson($message['msg'][0], $message['msg'][1]);
        }

        if (isset($data['room']) && is_array($data['room'])) {
            // 最多三个房间
            if (count($data['room']) > 3) {
                return parent::returnJson(403, 605);
            }

            $validateRule = array(
                'room' => 'notempty,id',
            );
            $validatePass = $this->validateUtil->validate($data['room'], $validateRule);
            if (empty($validatePass)) {
                $message = $this->validateUtil->getError('explode', ',');
                return parent::returnJson($message['msg'][0], $message['msg'][1]);
            }
        }

        $today = date('Y-m-d');
        $startDate = $endDate = $today;
        
        // 检查客栈是否已经开通微预定
        $hotelId = $data['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId, false);
        if (empty($isEnable)) {
            return parent::returnJson(403, 409);
        }

        // 检查房型是否可预定
        $roomTypeId = explode(',', $data['roomType']);
        $roomTypeBookLogic = A('Common/RoomTypeBook', 'Logic');
        $canBook = $roomTypeBookLogic->checkBookable($hotelId, $roomTypeId);
        if (empty($canBook)) {
            return parent::returnJson(403, 304);
        }
        // 检查房型房间是否对应
        $roomId = null;
        if (isset($data['room'])) {
            $roomId = $data['room'];
            $roomInRoomType = $roomTypeBookLogic->checkRoomInRoomType($hotelId, $roomTypeId[0], $roomId);
            if (empty($roomInRoomType)) {
                return parent::returnJson(403, 202);
            }
            //$roomId = explode(',', $roomId);
        }
        $hour = $data['hour'];
        $hourRoomLogic = A('Common/HourRoom', 'Logic');
        // 获取客栈单个房型实时房态
        try {
            $roomInfo = $hourRoomLogic->getRealtimeStatus($hotelId, $startDate, $endDate, $hour, false, $roomTypeId[0], $roomId);
        } catch (\Exception $e) {
            $errorMsg = explode(',', $e->getMessage());
            return parent::returnJson($errorMsg[0], $errorMsg[1]);
        }

        // 根据是否传房间ID,分别处理查询是房型还是房间是否可以预定
        if (is_null($roomId)) {
            if ($roomInfo[0]['rest'] == 0) {
                return parent::returnJson(403, 307);
            }
        } else {
            foreach ($roomInfo[0]['room'] as $key => $roomItem) {
                if ($roomItem['canBook'] == DISABLE) {
                    return parent::returnJson(403, 306);
                }
            }
        }

        // 获取客栈信息
        $hotelDetail = $hotelBookLogic->getInfo($hotelId, 'brief');

        // 获取客栈设置押金
        $hotelPayDetailModel = D('HotelPayDetail');
        $hotelPayDetail = $hotelPayDetailModel->getInfoByHotel($hotelId);
        $totalPrice = $roomInfo[0]['price'];
        // 多个房间 计算总价格 = 预定的房间数 * 一个房间每天不同价格之和
        if (isset($data['room']) && is_array($roomId)) {
            $totalPrice = $roomInfo[0]['price'] * count($roomId);
            $hotelPayDetail['deposit'] = $hotelPayDetail['deposit'] * count($roomId);
        }
        $hourRoomInitModel = D('HourRoomInit');
        $hourRoom = $hourRoomInitModel->getInfo($hotelId);
        // 根据是否查看房间,返回不同信息
        $returnData = array(
            'hotel' => $hotelId,
            'hotelName' => $hotelDetail['name'],
            'bookMode' => $hotelDetail['bookmode'],
            'roomType' => $roomInfo[0]['roomtype_id'],
            'roomTypeName' => $roomInfo[0]['name'],
            'price' => (float)$totalPrice,
            'deposit' => (float)$hotelPayDetail['deposit'],
            'hourroom' => $hourRoom
        );
        if (!is_null($roomId)) {
            $returnData['rooms'] = array();
            foreach ($roomInfo[0]['room'] as $key => $roomItem) {
                $roomInfos = array(
                    'room' => $roomItem['id'],
                    'roomName' => $roomItem['name'],
                );
                array_push($returnData['rooms'], $roomInfos);
            }
        }
        return parent::returnJson(200, 200, $returnData);
    }

    /**
     * 获取客栈所有长租房实时房态
     * @modify zhangzhen 2017-07-28    创建
     */
    public function longRoomTypes($data) {
        // 设置时区
        parent::switchTimeUtc($data);

        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }

        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'startDate' => 'notnull,notempty,isDate',
            'month' => 'notnull,notempty',
        );
        if (isset($data['transCurrencyId']) && !empty($data['transCurrencyId'])) {
            $validateRule['transCurrencyId'] = 'notnull,notempty,id';
        }
        $validatePass = $this->validateUtil->validate($data, $validateRule);
        if (empty($validatePass)) {
            $message = $this->validateUtil->getError('explode', ',');
            return parent::returnJson($message['msg'][0], $message['msg'][1]);
        }

        $today = date('Y-m-d');
        $month = $data['month'];
        $startDate = $data['startDate'];
        $endDate = date('Y-m-d',strtotime('+' . $month . 'months', strtotime($startDate)));
        // 是否从钥匙包进入
        $qrclietBook = $data['qrclietBook'];
        // 是否微信浏览器
        $isWechatBrowser = $data['isWechatBrowser'];
        $transCurrencyId = isset($data['transCurrencyId']) && !empty($data['transCurrencyId']) ? $data['transCurrencyId'] : null;
        $bookingLangSet = $data['bookingLangSet'];

        // 校验时间合法性
        if (isBeforeTime($startDate, $today) || isBeforeTime($endDate, $startDate)) {
            return parent::returnJson(403, 202);
        }

        // 检查客栈是否已经开通微预定
        $hotelId = $data['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId, false);
        if (empty($isEnable)) {
            return parent::returnJson(403, 409);
        }

        // 获取客栈所有房型实时房态
        $longRoomLogic = A('Common/LongRoom', 'Logic');
        try {
            $roomTypeInfo = $longRoomLogic->getRealtimeStatus($hotelId, $startDate, $endDate, $month);
        } catch (\Exception $e) {
            $errorMsg = explode(',', $e->getMessage());
            return parent::returnJson($errorMsg[0], $errorMsg[1]);
        }

        /*// 获取客栈支付方式
        $hotelPayDetailModel = D('HotelPayDetail');
        $hotelPayDetail = $hotelPayDetailModel->getInfoByHotel($hotelId);
        $isOffline = true;
        if (! empty($hotelPayDetail['type']) && $hotelPayDetail['type']>0){
            $isOffline = false;
        }*/

        // 是否开启支付方式 (是否线下付)
        $isOffline = $this->isOffline($hotelId, $qrclietBook, $isWechatBrowser);

        // 处理数据
        foreach ($roomTypeInfo as &$roomTypeItem) {
            sort($roomTypeItem['priceList'], SORT_NUMERIC);
            sort($roomTypeItem['shopPriceList'], SORT_NUMERIC);
            //$charityPrice = $isPayUclbrt == true ? $roomTypeItem['charityprice'] : '0.00';
            // 如果预定价格等于门市价格 则只取预定价格
            $roomTypeItem = array(
                'id' => $roomTypeItem['roomtype_id'],
                'coverImageUrl' => MEIZHU_URL . $roomTypeItem['coverimageurl'],
                'name' => $roomTypeItem['name'],
                'rest' => $roomTypeItem['rest'],
                'description' => $roomTypeItem['description'],
                'isOffline' => $isOffline,
                'price' => $roomTypeItem['price'],
                'charityPrice' => 0,
            );
        }
        unset($roomTypeItem);

        // 获取客栈信息
        $hotelDetail = $hotelBookLogic->getInfo($hotelId, 'brief');

        // 获取客栈货币
        $currencyLogic = A('Common/Currency', 'Logic');
        $hotelCurrencyInfo = $currencyLogic->takeHotelCurrency($hotelId, false, $bookingLangSet);

        // 价格汇率转换
        if (!empty($hotelCurrencyInfo) && !is_null($transCurrencyId) && !empty($roomTypeInfo)) {
            // 取出默认货币
            $defaultCurrency = my_sort($hotelCurrencyInfo, 'type', SORT_DESC, SORT_NUMERIC);
            $currencyId = $defaultCurrency[0]['type'] == 1 ? $defaultCurrency[0]['currencyId'] : 8 ; // 8 = 人民币
            if ($currencyId != $transCurrencyId) {
                $roomTypeInfo = $currencyLogic->exchangeRates($currencyId, $transCurrencyId, $roomTypeInfo, 'price');
            }
        }

        return parent::returnJson(200, 200, array(
            'hotel' => $hotelId,
            'hotelName' => $hotelDetail['name'],
            'bookMode' => $hotelDetail['bookmode'],
            'roomTypes' => $roomTypeInfo,
            'hotelCurrency' => $hotelCurrencyInfo,
        ));
    }

    /**
     * 获取客栈所有房型实时房态
     * @modify zhangzhen 2017-07-28    创建
     */
    public function longRoomTypeDetail($data) {
        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }
        parent::switchTimeUtc($data);
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'startDate' => 'notnull,notempty',
            'month' => 'notnull,notempty',
            'roomType' => 'notnull,notempty,id',
        );
        $validatePass = $this->validateUtil->validate($data, $validateRule);
        if (empty($validatePass)) {
            $message = $this->validateUtil->getError('explode', ',');
            return parent::returnJson($message['msg'][0], $message['msg'][1]);
        }
        $today = date('Y-m-d');
        $month = $data['month'];
        $startDate = $data['startDate'];
        $endDate = date('Y-m-d',strtotime('+' . $month . 'months', strtotime($startDate)));
        // 校验时间合法性
        if (isBeforeTime($startDate, $today) || isBeforeTime($endDate, $startDate)) {
            return parent::returnJson(403, 202);
        }
        // 检查客栈是否已经开通微预定
        $hotelId = $data['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId, false);
        if (empty($isEnable)) {
            return parent::returnJson(403, 409);
    }
        // 检查房型是否可预定
        $roomTypeId = $data['roomType'];
        $roomTypeBookLogic = A('Common/RoomTypeBook', 'Logic');
        $canBook = $roomTypeBookLogic->checkBookable($hotelId, explode(',', $data['roomType']));
        if (empty($canBook)) {
            return parent::returnJson(403, 304);
        }
        // 获取客栈所有房型实时房态
        $longRoomLogic = A('Common/LongRoom', 'Logic');
        // 获取客栈单个房型实时房态
        try {
            $roomTypeInfo = $longRoomLogic->getRealtimeStatus($hotelId, $startDate, $endDate, $month,  true, $roomTypeId);
        } catch (\Exception $e) {
            $errorMsg = explode(',', $e->getMessage());
            return parent::returnJson($errorMsg[0], $errorMsg[1]);
        }

        $roomTypeInfo[0]['gallery'] = array_column($roomTypeInfo[0]['gallery'], 'name');
        foreach ($roomTypeInfo[0]['gallery'] as &$urlItem) {
            $urlItem = MEIZHU_URL . $urlItem;
        }
        unset($urlItem);

        $hotelPayDetailModel = D('HotelPayDetail');
        $hotelPayDetail = $hotelPayDetailModel->getInfoByHotel($hotelId);

        // 获取客栈信息
        $hotelDetail = $hotelBookLogic->getInfo($hotelId, 'brief');

        $returnData = array(
            'hotel' => $hotelId,
            'hotelName' => $hotelDetail['name'],
            'bookMode' => $hotelDetail['bookmode'],
            'roomType' => $roomTypeInfo[0]['roomtype_id'],
            'roomTypeName' => $roomTypeInfo[0]['name'],
            'gallery' => $roomTypeInfo[0]['gallery'],
            'description' => $roomTypeInfo[0]['description'],
            'price' => $roomTypeInfo[0]['price'],
            'charityPrice' => 0,
        );

        // 房间预定模式,处理数据
        if ($hotelDetail['bookmode'] == BOOK_MODE_ROOM) {
            foreach ($roomTypeInfo[0]['room'] as &$roomItem) {
                $roomItem = array(
                    'enable' => $roomItem['canBook'],
                    'id' => $roomItem['id'],
                    'name' => $roomItem['name'],
                    'cardType' => $roomItem['roomfrom'],
                    'isDirty' => $roomItem['isdirty'],
                );
            }
            unset($roomItem);
            $returnData['rooms'] = $roomTypeInfo[0]['room'];
        }

        return parent::returnJson(200, 200, $returnData);
    }

    /**
     * 获取房间实时房态
     * @modify zhangzhen 2017-07-28    创建
     */
    public function longBookDetail($data) {
        if (empty(parent::checkRPCRight($data))) {
            return parent::returnJson(403, 202);
        }
        parent::switchTimeUtc($data);
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'startDate' => 'notnull,notempty',
            'month' => 'notnull,notempty',
            'roomType' => 'notnull,notempty,id',
        );
        $validatePass = $this->validateUtil->validate($data, $validateRule);
        if (empty($validatePass)) {
            $message = $this->validateUtil->getError('explode', ',');
            return parent::returnJson($message['msg'][0], $message['msg'][1]);
        }
        if (isset($data['room']) && is_array($data['room'])) {
            // 最多三个房间
            if (count($data['room']) > 3) {
                return parent::returnJson(403, 605);
            }

            $validateRule = array(
                'room' => 'notempty,id',
            );
            $validatePass = $this->validateUtil->validate($data['room'], $validateRule);
            if (empty($validatePass)) {
                $message = $this->validateUtil->getError('explode', ',');
                return parent::returnJson($message['msg'][0], $message['msg'][1]);
            }
        }

        $month = $data['month'];
        $startDate = $data['startDate'];
        $endDate = date('Y-m-d',strtotime('+' . $month . 'months', strtotime($startDate)));
        $today = date('Y-m-d');
        // 校验时间合法性
        if (isBeforeTime($startDate, $today) || isBeforeTime($endDate, $startDate)) {
            return parent::returnJson(403, 202);
        }

        // 检查客栈是否已经开通微预定
        $hotelId = $data['hotel'];
        $hotelBookLogic = A('Common/HotelBook', 'Logic');
        $isEnable = $hotelBookLogic->checkEnable($hotelId, false);
        if (empty($isEnable)) {
            return parent::returnJson(403, 409);
        }

        // 检查房型是否可预定
        $roomTypeId = explode(',', $data['roomType']);
        $roomTypeBookLogic = A('Common/RoomTypeBook', 'Logic');
        $canBook = $roomTypeBookLogic->checkBookable($hotelId, $roomTypeId);
        if (empty($canBook)) {
            return parent::returnJson(403, 304);
        }
        // 检查房型房间是否对应
        $roomId = null;
        if (isset($data['room'])) {
            $roomId = $data['room'];
            $roomInRoomType = $roomTypeBookLogic->checkRoomInRoomType($hotelId, $roomTypeId[0], $roomId);
            if (empty($roomInRoomType)) {
                return parent::returnJson(403, 202);
            }
            //$roomId = explode(',', $roomId);
        }

        $longRoomLogic = A('Common/LongRoom', 'Logic');
        // 获取客栈单个房型实时房态
        try {
            $roomInfo = $longRoomLogic->getRealtimeStatus($hotelId, $startDate, $endDate, $month, false, $roomTypeId[0], $roomId);
        } catch (\Exception $e) {
            $errorMsg = explode(',', $e->getMessage());
            return parent::returnJson($errorMsg[0], $errorMsg[1]);
        }

        // 根据是否传房间ID,分别处理查询是房型还是房间是否可以预定
        if (is_null($roomId)) {
            if ($roomInfo[0]['rest'] == 0) {
                return parent::returnJson(403, 307);
            }
        } else {
            foreach ($roomInfo[0]['room'] as $roomItem) {
                if ($roomItem['canBook'] == DISABLE) {
                    return parent::returnJson(403, 306);
                }
            }
        }

        // 获取客栈信息
        $hotelDetail = $hotelBookLogic->getInfo($hotelId, 'brief');

        // 获取客栈设置押金
        $hotelPayDetailModel = D('HotelPayDetail');
        $hotelPayDetail = $hotelPayDetailModel->getInfoByHotel($hotelId);
        $totalPrice = $roomInfo[0]['price'];
        // 多个房间 计算总价格 = 预定的房间数 * 一个房间每天不同价格之和
        if (isset($data['room']) && is_array($roomId)) {
            $totalPrice *= count($roomId);
            $hotelPayDetail['deposit'] *= count($roomId);
        }

        // 根据是否查看房间,返回不同信息
        $returnData = array(
            'hotel' => $hotelId,
            'hotelName' => $hotelDetail['name'],
            'bookMode' => $hotelDetail['bookmode'],
            'roomType' => $roomInfo[0]['roomtype_id'],
            'roomTypeName' => $roomInfo[0]['name'],
            'price' => (float)$totalPrice,
            'deposit' => (float)$hotelPayDetail['deposit'],
        );
        if (!is_null($roomId)) {
            $returnData['rooms'] = array();
            foreach ($roomInfo[0]['room'] as $key => $roomItem) {
                $roomInfos = array(
                    'room' => $roomItem['id'],
                    'roomName' => $roomItem['name'],
                );
                array_push($returnData['rooms'], $roomInfos);
            }
        }
        return parent::returnJson(200, 200, $returnData);
    }

    /**
     * 是否开启支付方式 (是否线下付)
     * @modify wenhui 2017-11-30    创建
     * @param intetger $hotelId 客栈id
     * @param bool $qrclietBook 是否从钥匙包进入
     * @param bool $isWechatBrowser 是否微信浏览器
     */
    private function isOffline ($hotelId, $qrclietBook, $isWechatBrowser) {
        $hotelPayDetailModel = D('HotelPayDetail');
        $hotelPayUsableModel = D('HotelPayUsable');

        $hotelPayDetail = $hotelPayDetailModel->getInfoByHotel($hotelId);
        $hotelPayUsableInfo = $hotelPayUsableModel->getInfo($hotelId);
        $currentDate = date('Y-m-d');
        $enablepay = false;
        if (($hotelPayUsableInfo['uclbrtalipayenable'] == 1 && $hotelPayUsableInfo['uclbrtalipayenddate'] >= $currentDate) || ($hotelPayUsableInfo['uclbrtwechatenable'] == 1 && $hotelPayUsableInfo['uclbrtwechatenddate'] >= $currentDate) || ($hotelPayUsableInfo['hotelalipayenable'] == 1 && $hotelPayUsableInfo['hotelalipayenddate'] >= $currentDate) || ($hotelPayUsableInfo['hotelwechatenable'] == 1 && $hotelPayUsableInfo['hotelwechatenddata'] >= $currentDate)) {
            $enablepay = true;
        }

        $isOffline = true;
        if (!empty($hotelPayDetail['type']) && $hotelPayDetail['type']>0 && $enablepay){
            $isOffline = false;
        }

        // 从钥匙包进入 没有商家微信自收
        if ($qrclietBook == true) {
            if (!empty($hotelPayDetail['type']) && $hotelPayDetail['type'] == 32 && $enablepay) {
                $isOffline = true;
            }

        }

        // 是否微信浏览器
        if (!$isWechatBrowser && !$qrclietBook) {
            if (!empty($hotelPayDetail['type']) && in_array($hotelPayDetail['type'], [2,32]) && $enablepay) {
                $isOffline = true;
            }

        }

        $currencyLogic = A('Common/Currency', 'Logic');
        $hotelDefaultCurrency = $currencyLogic->takeHotelCurrency($hotelId, true);
        // 默认货币不是人民币的客栈 则线下付 // 8 = 人民币
        if (!empty($hotelDefaultCurrency) && $hotelDefaultCurrency[0]['currencyId'] != 8) {
            $isOffline = true;
        }

        return $isOffline;
    }
}
